/* Author: 
 * Wilson Santos 
 */

var rp_added_count=0; // number of related posts already added
var rp_added = new Array(); // posts added (id list)
var num_posts=5;  // maximum number of related posts to be shown
//var related_category_list=["Tomb Raider", "Krita"];

/* 
 * Show related post using JSON feed and related categories
 */
function showRelatedPosts(json) {
  for (i=0; i<json.feed.entry.length && rp_added_count<num_posts; ++i) {
    post = json.feed.entry[i];
    post_id = post.id.$t;
    post_href = post.link[4].href;
    if(jQuery.inArray(post_id, rp_added)==-1 && post_href!=document.URL) {
      console.log(post_href);
      console.log(document.URL);
    
    post_title = post.title.$t;
    if (typeof post.media$thumbnail != 'undefined') {
      post_thumb = post.media$thumbnail.url;
    }
    /* related post html */
    var related_post_html='<li>';
    /* add thumbnail html to related post */
    if (show_rp_thumb) { 
      related_post_html+='<a class="related-post-thumb" href="'+post_href+'" title="'+post_title+'">'
        + '<img src="'+post_thumb+'"/>'
        + '</a>';
    }
    /* add title html to related post */
    if (show_rp_title) {
      related_post_html+='<div class="related-post-desc">'
        +'<a href="'+post_href+'" title="'+post_title+'">'
        + post_title
        + '</a>'
        + '</div>'
        + '</li>';
    }

    jQuery('#section-related-posts ul').append(
      jQuery(related_post_html)
    );
    rp_added.push(post_id);
    ++rp_added_count;
    }
  }  
}

function addRelatedPosts() {
jQuery('.entry-content').append('<section id="section-related-posts"><h4>You may also enjoy:</h4><ul></ul><div class="clear"></div></section>');

if (typeof related_category_list == 'undefined') {
  related_category_list = new Array();
  jQuery('.post-labels a').each(function() {
    related_category_list.push(jQuery(this).text());
  });
}
/* Add the JSON feed for each related category */
for(i=0; i<related_category_list.length; ++i) {
  document.write('<script src="http://'+blog_url+'.blogspot.com/feeds/posts/default/-/'+related_category_list[i]+'?alt=json-in-script&callback=showRelatedPosts&start-index=1"></script>');
}
}






